﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using TiaoTiaoCode.AutoUpdate.ServerUI.Models;

namespace TiaoTiaoCode.AutoUpdate.ServerUI.MainFrame
{
    public class MainViewModel : BindableBase
    {
        #region 字段 

        readonly IRegionManager _regionManager;

        IRegionNavigationJournal _journal = default!;

        #endregion

        #region 属性

        private ObservableCollection<MenuModel> menus = default!;

        /// <summary>
        /// 系统功能菜单
        /// </summary>
        public ObservableCollection<MenuModel> Menus
        {
            get { return menus; }
            set { SetProperty(ref menus, value); }
        }

        #endregion

        #region 命令

        ICommand? navigateCommand;

        /// <summary>
        /// 功能导航
        /// </summary>
        public ICommand NavigateCommand => navigateCommand ?? (navigateCommand = new DelegateCommand<MenuModel>(Navigate));

        #endregion

        public MainViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            Menus = new ObservableCollection<MenuModel>(new List<MenuModel>
            {
                new MenuModel{MenuName="应用程序管理",IconKind="Note",NavigateName="MainProgramManagerView"},
                new MenuModel{MenuName="版本发布",IconKind="Publish",NavigateName="MainProgramPublishView"}
            });

            //Navigate(Menus.First());
        }

        #region 命令实现

        /// <summary>
        /// 功能菜单导航
        /// </summary>
        /// <param name="menu"></param>
        void Navigate(MenuModel menu)
        {
            if (_regionManager == null) return;
            if (menu == null || string.IsNullOrWhiteSpace(menu.NavigateName)) return;

            _regionManager.Regions["MainContent"].RequestNavigate(menu.NavigateName, back =>
            {
                _journal = back.Context.NavigationService.Journal;
            });
        }

        #endregion
    }
}
